import router from '@/router'
import store from '@/store'
// 页面访问权限
const weiteArr = ['/login', '/404']
router.beforeEach(async(to, from, next) => {
  // console.log(to.path)
  if (store.getters.token) {
    if (to.path === '/login') {
      next('/')
    } else {
      if (!store.getters.userinfo.id) {
        await store.dispatch('user/getUserInfo')
        // //
        // console.log('登录后的menus', store.state.user.userinfo.menus)
        // console.log('登录后的points', store.state.user.userinfo.points)
        const res = await store.dispatch('permission/filterRoutes', store.state.user.userinfo.menus)
        store.commit('permission/setRoutes', res)
        // console.log('动态路由', res)
        router.addRoutes([...res, { path: '*', redirect: '/404', hidden: true }])
        router.push(to.path)
      }
      next()
    }
  } else {
    if (weiteArr.indexOf(to.path) !== -1) {
      next()
    } else {
      next('/login')
    }
  }
})
